{{ define "book-menu" }}

{{- $first := false -}}
{{- $current_node := .current_node -}}
{{- $is_root := .is_root -}}
{{- $order_by := .order_by -}}
{{ $icon := "" }}

{{ with .sect }}
  {{ if .IsSection }}
    {{ if not $is_root }}
      {{- $first = (eq $current_node.FirstSection.Type "book") | and (eq $current_node.FirstSection .) -}}
    {{ end }}

    {{- safeHTML $current_node.FirstSection.Params.pre_nav -}}

    {{/* Get section icon. */}}
    {{ $pack := or .Params.icon_pack "fas" }}
    {{ $pack_prefix := $pack }}
    {{ if in (slice "fab" "fas" "far" "fal") $pack }}
      {{ $pack_prefix = "fa" }}
    {{ end }}
    {{ with .Params.icon }}
      {{- if eq $pack "emoji" -}}
        {{- . | emojify -}}
      {{- else if eq $pack "custom" -}}
        {{- $svg_icon := resources.Get (printf "media/icons/%s.svg" .) -}}
        {{- if $svg_icon -}}
          {{ $icon = printf "<img src=\"%s\" alt=\"%s\" class=\"svg-icon svg-baseline pr-1\">" $svg_icon.RelPermalink . }}
        {{- end -}}
      {{- else -}}
        {{ $icon = printf "<i class=\"%s %s-%s pr-1\"></i>" $pack $pack_prefix . }}
      {{- end -}}
    {{ end }}

    {{ if $first }}
      <ul class="nav docs-sidenav">
        <li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle }}</a></li>
    {{else}}
      <div class="docs-toc-item">
        <a class="docs-toc-link {{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }} active{{ end }}" href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle }}</a>
    {{end}}

    {{- $page_count := (add (len .Pages) (len .Sections)) -}}
    {{ if ne $page_count 0 }}
      {{ if not $first }}
        <ul class="nav docs-sidenav">
      {{end}}
      {{- .Scratch.Set "pages" .Pages -}}
      {{- if .Sections -}}
        {{- .Scratch.Set "pages" (.Pages | union .Sections) -}}
      {{- end -}}
      {{- $pages := (.Scratch.Get "pages") -}}
      {{- if eq $order_by "title" -}}
        {{- range $pages.ByTitle -}}
          {{ template "book-menu" dict "sect" . "current_node" $current_node "order_by" $order_by }}
        {{- end -}}
      {{- else if eq $order_by "title_desc" -}}
        {{- range $pages.ByTitle.Reverse -}}
          {{ template "book-menu" dict "sect" . "current_node" $current_node "order_by" $order_by }}
        {{- end -}}
      {{- else -}}
        {{- range $pages.ByWeight -}}
          {{ template "book-menu" dict "sect" . "current_node" $current_node "order_by" $order_by }}
        {{- end -}}
      {{- end}}
      {{ if not $first }}
        </ul>
      {{end}}
    {{end}}

    {{ if not $first }}
      </div>
    {{else}}
      </ul>
    {{end}}

  {{- else -}}
    {{- if not .Params.Hidden -}}
      <li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default (title .File.ContentBaseName) }}</a></li>
    {{- end -}}
  {{ end -}}
{{- end -}}

{{/* End define. */}}
{{ end -}}
